/**
 * Contient un workflow complet.
 */
package ch.ifage.business.dom;

import java.io.Serializable;

/**
 * @author Christian
 * 
 */
public class Workflow implements Serializable {

	/**
	 * serial id.
	 */
	private static final long serialVersionUID = 5987971314479749192L;

	/**
	 * Première étape d'un workflow.
	 */
	private FirstStep firstStep = null;

	/**
	 * @param firstStep
	 *            premier traitement d'un workflow.
	 */
	public Workflow(final FirstStep firstStep) {
		this.firstStep = firstStep;
	}

	/**
	 * @return FirstStep.
	 */
	public final FirstStep getFirstStep() {
		return firstStep;
	}

	/**
	 * Boolean that indicate if one of the whole workflow contains a specific string.
	 * 
	 * @return true if one of the whole workflow contains a specific string.
	 * @param subStringOfLog
	 *            string to find in the logs.
	 */
	public final boolean logContainsSubString(final String subStringOfLog) {
		boolean result = this.firstStep.getProperties().getLog().toLowerCase().contains(subStringOfLog);
		if (result) {
			return result;
		} else {
			Integer childIndex = this.firstStep.getListOfChild().size();
			while (!result && (childIndex > 0)) {
				childIndex -= 1;
				result = this.firstStep.getListOfChild().get(childIndex).logContainsSubString(subStringOfLog);
			}
			return result;
		}
	}

	/**
	 * Return the fist strep that contains a specific string.
	 * 
	 * @return the step found or null if not found.
	 * @param subStringOfLog
	 *            string to find in the logs. This functionnality is used by the SOAP server.
	 */
	public final Step getStepLogContainsSubString(final String subStringOfLog) {
		if (this.firstStep.getProperties().getLog().toLowerCase().contains(subStringOfLog)) {
			return this.firstStep;
		}
		Integer childIndex = this.firstStep.getListOfChild().size();
		while (childIndex > 0) {
			childIndex -= 1;
			Step curStep = this.firstStep.getListOfChild().get(childIndex);
			Step foundStep = curStep.getStepLogContainsSubString(subStringOfLog);
			if (foundStep != null) {
				return foundStep;
			}
		}
		return null;
	}

	/**
	 * @return string.
	 */
	@Override
	public final String toString() {
		return "Workflow (firstStep=" + firstStep + ")\n";
	}

}
